<template>
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%;">
    <el-col>
      <!-- 顶层的节点时候 -->
      <i v-if="isRoot" class="el-icon-s-home" />
      <!-- 不是根的时候 -->
      <span v-else>
        <!-- 当树节点的时候 -->
        <i v-if="!treeNode.children || treeNode.children.length===0" class="el-icon-user-solid" />
        <span v-else>
          <!-- 打开了 -->
          <i v-if="treeJiedian.expanded" class="el-icon-remove-outline" />
          <!-- 关闭了 -->
          <i v-else class="el-icon-circle-plus-outline" />
        </span>
      </span>
      <span>{{ treeNode.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <el-dropdown @command="operateDepts">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="$emit('addDepts',treeNode)">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from '@/api/departments'
export default {
  name: 'TreeTools',
  props: {
    // 这一行的数据
    treeNode: {
      type: Object,
      required: true
    },
    // 一行的节点信息
    treeJiedian: {
      type: Object,
      default: () => {}
    },
    // 是否作为根节点
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    // 下拉点击事件
    async operateDepts(val) {
      console.log(val)
      if (val === 'edit') {
        // 修改操作
        this.$emit('editDepts', this.treeNode)
      } else if (val === 'del') {
        // 删除操作
        // 1. 确认框
        try {
          await this.$confirm('确定删除吗？')
        } catch (error) {
          return console.log(error)
        }
        // 2. 发送删除请求
        await delDepartments(this.treeNode.id)
        // 3. 提示框
        this.$message.success('删除成功')
        // 4. 通知父组件更新
        this.$emit('delDepts')
      }
    }
  }
}
</script>

<style>

</style>
